package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.TokenBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.auth.TokenEntity;
import com.hst.capacity.domain.service.TokenService;
import com.hst.capacity.infra.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Service
public class TokenBizImpl implements TokenBiz {

    @Autowired
    TokenService tokenService;

    @Override
    public String findIdUser(HttpServletRequest request) {
        String idDispatcher = null;
        String accessToken = request.getHeader("Access-Token");
        boolean isexpired = false;
        if (StringUtil.isNotBlank(accessToken)) {
            log.info("accessToken" + accessToken);
            TokenEntity tokenEntity = tokenService.getAccessToken(accessToken);
            if (tokenEntity != null) {
                idDispatcher = tokenEntity.getUserId();
            } else {
                isexpired = true;
            }
        } else {
            isexpired = true;
        }

        if (isexpired)
            throw new BusinessException(4002,"Token已失效，请重新登录。");

        return idDispatcher;
    }

}
